<template>
  <div class="cart">
    <el-table :data="$store.state.cart"
              border
              style="width: 100%">
      <el-table-column type="index"
                       label="序号"
                       width="50"
                       align="center"></el-table-column>
      <el-table-column prop="name"
                       label="书名"
                       align="center"></el-table-column>
      <el-table-column prop="price"
                       label="价格"
                       width="100"
                       align="center"></el-table-column>
      <el-table-column label="数量"
                       align="center">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.number"
                           :min="1"
                           :max="scope.row.pnum"
                           label="描述文字"
                           @change="change(scope.row.number,scope.$index)"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="小计"
                       width="150"
                       align="center">
        <template slot-scope="scope">
          {{scope.row.lineTotal}}
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="180"
                       align="center">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="total_warpper">
      <h3>共计：{{$store.getters.totalPrice}}</h3>
      <h3>总数：{{$store.getters.totalNumber}}</h3>
    </div>
    <div>
      <h3>收货人信息</h3>
        <label>收货人电话：</label>
        <el-input v-model="info.phone" placeholder="请输入收货人的电话号码" type="number" minlength="11"/>
        <label>收货人姓名：</label>
        <el-input v-model="info.name" type="text"/>
        <label>收货人地址：</label>
        <el-input v-model="info.address" type="text"/>
         
    </div>
    <div class="group_btn">
      <el-button type="success"
                 @click="commitOrder()">确认订单，提交</el-button>
    </div>

  </div>
</template>
 
 <script>
import axios from 'axios'
import myapi from '../api'
export default { 
  data(){
    return{
      info:{phone:'',name:'',address:''}
    }
  }, 
  methods: {
    change(val,index){
      this.$store.dispatch('refresh',{'index':index,'val':val});
    },
    commitOrder() {//将订单数据提交给后端
      axios.post(myapi.commitOrder.url,{'cart':this.$store.state.cart,'info':this.info})
      .then(res=>{
        if(res.data.code===100){
          this.$message({
            showClose:true,
            message:'订单提交成功，请在2小时内支付！',
            type:'success',
            center:true
          });
          this.$router.push('/pay');
        }else{
          this.$message({
            showClose:true,
            message:'订单提交失败！'+res.data.msg,
            type:'error',
            center:true
          });
        }
      })
      .catch(res=>{
        this.$message({
            showClose:true,
            message:'订单提交失败！'+res.data,
            type:'error',
            center:true
            });
      });

    },
    handleDelete(index) {
      this.$store.dispatch('delGoodsByIndex', index)
    },
  },
}
</script>
 
 <style scoped>
.cart {
  margin: 50px auto;
  width: 1000px;
}
.group_btn {
  margin-bottom: 30px;
}
.addDialog .el-input {
  width: 90%;
}
.total_warpper {
  text-align: right;
  margin-top: 30px;
}
h3 {
  margin-top: 10px;
}
</style> 